<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>高效算法求解数独 | 博客主页 | 世界是个球，前方总有路！</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="数据结构与算法">
    <meta name="description" content="背景&amp;emsp;&amp;emsp;之前上python课的时候，有一次实验是求解数独，要求时间复杂度要低；为此老师讲解了一个高效的数独算法，我觉得算法挺有意思的，写篇博客记录一下。   描述首先需要知晓数独的两个规则：  若某个位置的值已经确定，那么，和这个位置在同一行，同一列，同一个3×3的格子，都不能填写这个值，比如，九宫格（1，1）位置的值为2，那么，第一行，第一列，以及第一个3×3的格子里，都不能">
<meta property="og:type" content="article">
<meta property="og:title" content="高效算法求解数独">
<meta property="og:url" content="http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/index.html">
<meta property="og:site_name" content="博客主页">
<meta property="og:description" content="背景&amp;emsp;&amp;emsp;之前上python课的时候，有一次实验是求解数独，要求时间复杂度要低；为此老师讲解了一个高效的数独算法，我觉得算法挺有意思的，写篇博客记录一下。   描述首先需要知晓数独的两个规则：  若某个位置的值已经确定，那么，和这个位置在同一行，同一列，同一个3×3的格子，都不能填写这个值，比如，九宫格（1，1）位置的值为2，那么，第一行，第一列，以及第一个3×3的格子里，都不能">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-12-26T09:55:16.000Z">
<meta property="article:modified_time" content="2019-12-26T10:26:04.313Z">
<meta property="article:author" content="特务依昂">
<meta property="article:tag" content="数据结构与算法">
<meta name="twitter:card" content="summary">
    
        <link rel="alternate" type="application/atom+xml" title="博客主页" href="/blog/atom.xml">
    
    <link rel="shortcut icon" href="/blog/img/title.png">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/brand.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">特务依昂</h5>
          <a href="mailto:1131564805@qq.com" title="1131564805@qq.com" class="mail">1131564805@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                博客
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/tewuyiang" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://weibo.com/u/5516635708/" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.cnblogs.com/tuyang1129/" target="_blank" >
                <i class="icon icon-lg icon-link"></i>
                博客园
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">高效算法求解数独</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">高效算法求解数独</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-12-26T09:55:16.000Z" itemprop="datePublished" class="page-time">
  2019-12-26
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/">数据结构与算法</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#背景"><span class="post-toc-number">1.</span> <span class="post-toc-text">背景</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#描述"><span class="post-toc-number">2.</span> <span class="post-toc-text">描述</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#求解步骤"><span class="post-toc-number">2.0.1.</span> <span class="post-toc-text">求解步骤</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#所需计算"><span class="post-toc-number">2.0.2.</span> <span class="post-toc-text">所需计算</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#代码"><span class="post-toc-number">3.</span> <span class="post-toc-text">代码</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#扩展"><span class="post-toc-number">4.</span> <span class="post-toc-text">扩展</span></a></li></ol>
        </nav>
    </aside>


<article id="post-高效算法求解数独"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">高效算法求解数独</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-12-26 17:55:16" datetime="2019-12-26T09:55:16.000Z"  itemprop="datePublished">2019-12-26</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/">数据结构与算法</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h2><p>&emsp;&emsp;之前上python课的时候，有一次实验是求解数独，要求时间复杂度要低；为此老师讲解了一个高效的数独算法，我觉得算法挺有意思的，写篇博客记录一下。</p>
<br>

<h2 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h2><p>首先需要知晓数独的两个规则：</p>
<ol>
<li>若某个位置的值已经确定，那么，和这个位置在同一行，同一列，同一个3×3的格子，都不能填写这个值，比如，九宫格（1，1）位置的值为2，那么，第一行，第一列，以及第一个3×3的格子里，都不能在填2了；</li>
<li>若某一行，或者某一列，或者某一个3×3里面，只有一个位置可能填1（假如是1），那么1一定是填写在这个位置，因为没有其他位置可以填它了；、</li>
</ol>
<br>

<h4 id="求解步骤"><a href="#求解步骤" class="headerlink" title="求解步骤"></a>求解步骤</h4><ol>
<li><p>创建一个三维数组，假设就叫“可能值数组”，记录数独9×9的81个位置中，每个位置可能填写的值，初始情况下，每个位置的可能值都是1到9，表示每个位置都可能填写1-9中任何一个数字；</p>
</li>
<li><p>遍历数独的每一个位置，若某个位置已经有值，则将这个位置的可能值更新为这个值，比如，九宫格上，（1，1）的值已经确定是2了，那就将三维数组中（1，1）位置的可能值从[1-9]更新为[2]，直到所有的位置更新完毕；</p>
</li>
<li><p>使用上述规则1进行剪枝：</p>
<blockquote>
<p>（1）：从第一个位置开始遍历九宫格，若当前遍历到的位置（i,j），它的值已经知晓，那么就更新可能值数组，将第i行，第j列，以及其对应的3×3（【i/3×3 , j/3×3】就是这个3×3的第一个点）的所有位置，它们的可能值都要去除（i，j）位置的值；</p>
<p>（2）：若某个位置在经过上一步的剪枝后，可能值只剩下一个了，那这个位置的值就确定了，比如说，位置（1，1）的初始可能值是1到9，经过上面的一步步去除，只剩下一个3了，那这个（1，1）位置填写的值必定就是3了。此时我们可以再次使用规则1，即第一行，第一列，以及其对应的3×3中，所有的格子的可能值不能有3；</p>
<p>（3）：依次遍历每一个位置，使用上面的规则1，直到最后一格子，第一次剪枝便完成了；</p>
</blockquote>
</li>
<li><p>使用上面的规则2进行剪枝：</p>
<blockquote>
<p>（1）：统计每一行，每一列，以及每一个3×3中，每个数出现的次数，比如统计第一行每个格子的可能值，看1-9各出现几次，若某个可能值只出现一次，那出现这个值的位置，就是填写这个值，比如说，在第一行，3这个数字，只有（1，1）这个位置可能填写，那（1，1）就是填3，因为其他位置的可能值当中都不包含3，也就是都不能填写3；</p>
<p>（2）：根据上一步确定了某个位置的值后，那我们此时又可以使用规则1了，比如，上一步确定了（1，1）是填写3，那么第一行，第一列，以及第一个3×3中其余的格子， 都不能在写3了，我们从可能值数组中，将这些位置的可能，值删除3这个数；</p>
<p>（3）：而此时，又可能出现上面的第3步中的（3）的情况；</p>
</blockquote>
</li>
<li><p>规则2剪枝完毕后，数独还没有解决完毕，那我们只能通过枚举每一个位置的值，来一一尝试，直到找到最后的解决办法：</p>
<blockquote>
<p>（1）：我们在最开始创建了一个三维数组，存储每一个位置的可能值，初始情况下，每个位置的可能值都是1-9，但是经过上面两个规则的剪枝后，已经去除了很多；</p>
<p>（2）：此时我们使用DFS深度优先搜索，尝试为每一个位置填值。经过上面的剪枝，每个位置的可能值的数量应该不一样了，而为了减少DFS搜索的次数，我们应该从可能值最少的位置开始搜索；</p>
<p>（3）：遍历9宫格，找出还未填写值，且可能值最少的那个位置（可能有多个，找出第一个），尝试将他的第一个可能值填写在这个位置，然后再次调用规则1和规则2进行剪枝，剪枝完毕后，判断当前的九宫格中，是否有不和规则的地址，比如同一行出现两个一样的数。若没有不合法的地方，则再次进行一次（3），若有，表示这个位置不能填这个值，则从这个位置的可能值中再选择另外一个；</p>
<p>（4）：一直使用步骤（3），直到所有的位置都确定，则表示成功解出数独，若有某个位置，它的任何一个可能值填上去，都不能得到最终结果，那数独就是无解的；</p>
</blockquote>
</li>
<li><p>经过上面这些步骤，就能快速的解出数独，因为主要通过规则1，2进行剪枝，大大减少了枚举的次数，提升了效率；</p>
</li>
</ol>
<br>

<h4 id="所需计算"><a href="#所需计算" class="headerlink" title="所需计算"></a>所需计算</h4><ol>
<li>已知位置（i，j），则这个位置所在的3*3，其第一个点是（i/3×3 , j/3×3），i/3×3表示先作除法，去除了小数部分，再乘3，就是3的倍数了；</li>
<li>已知位置（i，j），如何计算这个位置属于第几个3×3，那就是（i/3×3 + j/3），每个3*3都占3行，且3列，i/3得到这个位置在第几个3行，j/3得到这个位置在第几个3列，每三行有三个3×3，所以i/3×3 + j/3就可以得到这个位置在第几个3×3；</li>
</ol>
<br>

<h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><p>因为是为了完成python实验，所以代码是用python写的：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 此类用来表示搜索时，需要搜索的一个位置</span></span><br><span class="line"><span class="comment"># x，y为此位置的坐标，size为此位置的可能值的数量</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, x, y, size)</span>:</span></span><br><span class="line">        self.x = x</span><br><span class="line">        self.y = y</span><br><span class="line">        self.size = size</span><br><span class="line"></span><br><span class="line"><span class="comment"># 读取方式2，读取top95</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">read_way2</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="comment"># 从文件中读取初始数独</span></span><br><span class="line">    value = [[<span class="number">0</span>] * <span class="number">10</span> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>)]</span><br><span class="line">    <span class="comment"># 读取文件2，3</span></span><br><span class="line">    s = infile.readline();</span><br><span class="line">    <span class="keyword">if</span> s == <span class="string">''</span>:</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">            value[i][j] = int(s[i * <span class="number">9</span> + j])</span><br><span class="line">    <span class="keyword">return</span> value</span><br><span class="line"></span><br><span class="line"><span class="comment"># 初始化函数</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">init</span><span class="params">()</span>:</span></span><br><span class="line">    value = read_way2()   <span class="comment"># 读取top95</span></span><br><span class="line">    <span class="comment"># 初始化possibleValue，若当前位置有值，则其可能值就是这个值本身</span></span><br><span class="line">    <span class="comment"># 若没有值,则初始的可能值就是1-9</span></span><br><span class="line">    possibleValue = [[[] <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">10</span>)] <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>)]</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">            <span class="keyword">if</span> value[i][j] != <span class="number">0</span>:</span><br><span class="line">                possibleValue[i][j] = [value[i][j]]</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                possibleValue[i][j] = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> possibleValue</span><br><span class="line"></span><br><span class="line"><span class="comment">#####################################################################################################################</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据规则1进行剪枝</span></span><br><span class="line"><span class="comment"># 遍历所有的位置,找到已经确定的位置进行剪枝</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">pruningByRule1</span><span class="params">(possibleValue)</span>:</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">            <span class="keyword">if</span> len(possibleValue[i][j]) == <span class="number">1</span>:</span><br><span class="line">                removeValueByRule1(i, j, possibleValue)    <span class="comment"># 以当前位置为起点,移除重复的可能值</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 在规则1剪枝中,将同一区域中,已经确定的数移除</span></span><br><span class="line"><span class="comment"># 以(i,j)位置为起点,移除重复的可能值</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">removeValueByRule1</span><span class="params">(i, j, possibleValue)</span>:</span></span><br><span class="line">    <span class="comment"># 与当前值在同一行或同一列的位置，可能值减去当前位置的值</span></span><br><span class="line">    <span class="keyword">for</span> k <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">        <span class="comment"># 从第i行中的可能值列表中,移除当前值</span></span><br><span class="line">        confirmOneValueInRule1(i, k, possibleValue[i][j][<span class="number">0</span>], possibleValue)</span><br><span class="line">        <span class="comment"># 从第i列中的可能值列表中,移除当前值</span></span><br><span class="line">        confirmOneValueInRule1(k, j, possibleValue[i][j][<span class="number">0</span>], possibleValue)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 与当前值在同3*3的位置，可能值减去当前位置的值</span></span><br><span class="line">    <span class="keyword">for</span> k <span class="keyword">in</span> range(int(i / <span class="number">3</span>) * <span class="number">3</span>, int(i / <span class="number">3</span>) * <span class="number">3</span> + <span class="number">3</span>):</span><br><span class="line">        <span class="keyword">for</span> l <span class="keyword">in</span> range(int(j / <span class="number">3</span>) * <span class="number">3</span>, int(j / <span class="number">3</span>)* <span class="number">3</span> + <span class="number">3</span>):</span><br><span class="line">            confirmOneValueInRule1(k, l, possibleValue[i][j][<span class="number">0</span>], possibleValue)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 移除某个位置的可能值,并在移除后判断能否得到确定值</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">confirmOneValueInRule1</span><span class="params">(i, j, num, possibleValue)</span>:</span></span><br><span class="line">    <span class="keyword">if</span> len(possibleValue[i][j]) == <span class="number">1</span>:</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    <span class="comment"># 从当前位置的可能值中,移除已经确定的数</span></span><br><span class="line">    <span class="keyword">if</span> num <span class="keyword">in</span> possibleValue[i][j]:</span><br><span class="line">        possibleValue[i][j].remove(num)</span><br><span class="line">    <span class="comment"># 判断移除后,当前位置能否确定</span></span><br><span class="line">    <span class="keyword">if</span> len(possibleValue[i][j]) == <span class="number">1</span>:</span><br><span class="line">        <span class="comment"># 若当前位置确定，则以当前位置为基准进行移除操作</span></span><br><span class="line">        removeValueByRule1(i, j, possibleValue)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">###########################################################################################</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据规则2剪枝,判断同一个区域每个值可能出现的次数</span></span><br><span class="line"><span class="comment"># 若某个值可能出现的位置只有一个,表示这个值就在此位置</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">pruningByRule2</span><span class="params">(possibleValue)</span>:</span></span><br><span class="line">    <span class="comment"># 统计第i行，数字j可能值出现了几次</span></span><br><span class="line">    countX = [[<span class="number">0</span>] * <span class="number">10</span> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">12</span>)]</span><br><span class="line">    <span class="comment"># 统计第i列，数字j可能值出现了几次</span></span><br><span class="line">    countY = [[<span class="number">0</span>] * <span class="number">10</span> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">12</span>)]</span><br><span class="line">    <span class="comment"># 统计第i个3*3，数字j可能值出现了几次</span></span><br><span class="line">    countZ = [[<span class="number">0</span>] * <span class="number">10</span> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">12</span>)]</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 统计各个区域可能值出现的次数</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">            <span class="keyword">for</span> num <span class="keyword">in</span> possibleValue[i][j]:</span><br><span class="line">                countX[i][num] += <span class="number">1</span></span><br><span class="line">                countY[j][num] += <span class="number">1</span></span><br><span class="line">                countZ[i // <span class="number">3</span> * <span class="number">3</span> + j // <span class="number">3</span>][num] += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 判断哪些数字只出现了一次, 若只出现了一次的数字</span></span><br><span class="line">    <span class="comment"># 表示这个数字就是那个位置的答案</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">10</span>):</span><br><span class="line">            <span class="comment"># 若第i行数字j只出现了一次</span></span><br><span class="line">            <span class="keyword">if</span> countX[i][j] == <span class="number">1</span>:</span><br><span class="line">                <span class="keyword">for</span> k <span class="keyword">in</span> range(<span class="number">9</span>):  <span class="comment"># 遍历第i行的每一列，判断这个唯一值出现在哪</span></span><br><span class="line">                    confirmValueInRule2(i, k, j, possibleValue)</span><br><span class="line"></span><br><span class="line">            <span class="comment"># 若第i列数字j只出现了一次</span></span><br><span class="line">            <span class="keyword">if</span> countY[i][j] == <span class="number">1</span>:</span><br><span class="line">                <span class="keyword">for</span> k <span class="keyword">in</span> range(<span class="number">9</span>):  <span class="comment"># 遍历第i列的每一列，判断这个唯一值出现在哪</span></span><br><span class="line">                    confirmValueInRule2(k, i, j, possibleValue)</span><br><span class="line"></span><br><span class="line">            <span class="comment"># 若第i个3 * 3中，数字j的可能值只有一个</span></span><br><span class="line">            <span class="keyword">if</span> countZ[i][j] == <span class="number">1</span>:</span><br><span class="line">                <span class="comment"># 遍历第i个3*3的所有位置，判断这个唯一值出现在哪</span></span><br><span class="line">                <span class="keyword">for</span> k <span class="keyword">in</span> range(i//<span class="number">3</span>*<span class="number">3</span>, i//<span class="number">3</span>*<span class="number">3</span>+<span class="number">3</span>):</span><br><span class="line">                    <span class="keyword">for</span> l <span class="keyword">in</span> range(i%<span class="number">3</span>*<span class="number">3</span>, i%<span class="number">3</span>*<span class="number">3</span>+<span class="number">3</span>):</span><br><span class="line">                        confirmValueInRule2(k, l, j, possibleValue)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 判断当前位置是否包含某个数,包含则为此位置的答案</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">confirmValueInRule2</span><span class="params">(i, j, singleNum, possibleValue)</span>:</span></span><br><span class="line">    <span class="comment"># 若当前位置已经确定值了, 直接返回</span></span><br><span class="line">    <span class="keyword">if</span> len(possibleValue[i][j]) ==<span class="number">1</span>:</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    <span class="comment"># 若当前位置包含唯一可能值，则这个位置的确定值就是它</span></span><br><span class="line">    <span class="keyword">if</span> singleNum <span class="keyword">in</span> possibleValue[i][j]:</span><br><span class="line">            possibleValue[i][j] = [singleNum]</span><br><span class="line">            <span class="comment"># 重新调用规则1</span></span><br><span class="line">            removeValueByRule1(i, j, possibleValue)</span><br><span class="line"></span><br><span class="line"><span class="comment">###########################################################################################</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 递归搜索</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">searchForPruning</span><span class="params">(node, possibleValue)</span>:</span></span><br><span class="line">    <span class="comment"># 若没有需要填值的点了，表示搜索结束，答案已出</span></span><br><span class="line">    <span class="keyword">if</span> node <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">        <span class="keyword">return</span> possibleValue</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 获取当前位置的x，y坐标</span></span><br><span class="line">    x = node[<span class="number">0</span>]</span><br><span class="line">    y = node[<span class="number">1</span>]</span><br><span class="line">    <span class="keyword">for</span> num <span class="keyword">in</span> possibleValue[x][y]:</span><br><span class="line">        <span class="comment"># 复制一份当前状态</span></span><br><span class="line">        tempPossibleValue = copy.deepcopy(possibleValue)</span><br><span class="line">        <span class="comment"># 更新数据</span></span><br><span class="line">        tempPossibleValue[x][y] = [num]</span><br><span class="line">        <span class="comment"># 调用规则1，2</span></span><br><span class="line">        removeValueByRule1(x, y, tempPossibleValue)</span><br><span class="line">        pruningByRule2(tempPossibleValue)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 调用规则1，2后，判断当前结果是否合法，若合法，则进行递归下一层</span></span><br><span class="line">        <span class="keyword">if</span> judge_result(tempPossibleValue):</span><br><span class="line">            <span class="comment"># 递归求解</span></span><br><span class="line">            tempPossibleValue = searchForPruning(get_lowest_node(tempPossibleValue), tempPossibleValue)</span><br><span class="line">            <span class="comment"># 判断递归结果，若结果有返回值，则表示求解成功</span></span><br><span class="line">            <span class="keyword">if</span> tempPossibleValue <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">                <span class="keyword">return</span> tempPossibleValue</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取当前可能值最小的位置</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_lowest_node</span><span class="params">(possibleValue)</span>:</span></span><br><span class="line">    minn = <span class="number">100</span></span><br><span class="line">    node = <span class="literal">None</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">            <span class="comment"># 若当前位置没有确定值，并且可能值的数量更少，则更新记录，</span></span><br><span class="line">            <span class="keyword">if</span> <span class="number">1</span> &lt; len(possibleValue[i][j]) &lt; minn:</span><br><span class="line">                minn = len(possibleValue[i][j])</span><br><span class="line">                node = (i, j)</span><br><span class="line">    <span class="keyword">return</span> node</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 判断某个位置是否可以放某个值</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">judge_result</span><span class="params">(possibleValue)</span>:</span></span><br><span class="line">    <span class="comment"># 标记某个数字是否出现</span></span><br><span class="line">    countX = [[<span class="literal">False</span>] * <span class="number">10</span> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">12</span>)]</span><br><span class="line">    countY = [[<span class="literal">False</span>] * <span class="number">10</span> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">12</span>)]</span><br><span class="line">    countZ = [[<span class="literal">False</span>] * <span class="number">10</span> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">12</span>)]</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 统计各个区域可能值出现的次数</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">            <span class="keyword">if</span> len(possibleValue[i][j]) == <span class="number">1</span>:</span><br><span class="line">                <span class="comment"># 若当前状态不合法，返回false</span></span><br><span class="line">                <span class="keyword">if</span> countX[i][possibleValue[i][j][<span class="number">0</span>]] <span class="keyword">or</span> countY[j][possibleValue[i][j][<span class="number">0</span>]] <span class="keyword">or</span> countZ[i // <span class="number">3</span> * <span class="number">3</span> + j // <span class="number">3</span>][possibleValue[i][j][<span class="number">0</span>]]:</span><br><span class="line">                    <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">                <span class="comment"># 若合法，则标记已经确定的数字</span></span><br><span class="line">                countX[i][possibleValue[i][j][<span class="number">0</span>]] = <span class="literal">True</span></span><br><span class="line">                countY[j][possibleValue[i][j][<span class="number">0</span>]] = <span class="literal">True</span></span><br><span class="line">                countZ[i // <span class="number">3</span> * <span class="number">3</span> + j // <span class="number">3</span>][possibleValue[i][j][<span class="number">0</span>]] = <span class="literal">True</span></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 判断某个位置是否可以放某个值</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">judge_now_number</span><span class="params">(possibleValue, i, j, num)</span>:</span></span><br><span class="line">    <span class="comment"># 判断num在这一行和这一列是否被使用</span></span><br><span class="line">    <span class="keyword">for</span> k <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">        <span class="keyword">if</span> len(possibleValue[i][k]) == <span class="number">1</span> <span class="keyword">and</span> possibleValue[i][k][<span class="number">0</span>] == num:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">        <span class="keyword">if</span> len(possibleValue[k][j]) == <span class="number">1</span> <span class="keyword">and</span> possibleValue[k][j][<span class="number">0</span>] == num:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">    <span class="comment"># 判断num在这个3*3是否被使用</span></span><br><span class="line">    <span class="keyword">for</span> k <span class="keyword">in</span> range(int(i / <span class="number">3</span>) * <span class="number">3</span>, int(i / <span class="number">3</span>) * <span class="number">3</span> + <span class="number">3</span>):</span><br><span class="line">        <span class="keyword">for</span> l <span class="keyword">in</span> range(int(j / <span class="number">3</span>) * <span class="number">3</span>, int(j / <span class="number">3</span>) * <span class="number">3</span> + <span class="number">3</span>):</span><br><span class="line">            <span class="keyword">if</span> len(possibleValue[k][l]) == <span class="number">1</span> <span class="keyword">and</span> possibleValue[k][l][<span class="number">0</span>] == num:</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment">###########################################################################################</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 输出展示可能值列表</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">display</span><span class="params">(possibleValue)</span>:</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">9</span>):</span><br><span class="line">            print(possibleValue[i][j], end=<span class="string">"---"</span>)</span><br><span class="line">        print()</span><br><span class="line">    print()</span><br><span class="line"></span><br><span class="line"><span class="comment">###########################################################################################</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 主函数</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    start = time.time()</span><br><span class="line">    c = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 主逻辑</span></span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="comment"># 调用初始化函数</span></span><br><span class="line">        possibleValue = init()</span><br><span class="line">        <span class="comment"># 调用规则1剪枝</span></span><br><span class="line">        pruningByRule1(possibleValue)</span><br><span class="line">        <span class="comment"># 调用规则2剪枝</span></span><br><span class="line">        pruningByRule2(possibleValue)</span><br><span class="line">        <span class="comment"># display(possibleValue)</span></span><br><span class="line"></span><br><span class="line">        possibleValue = searchForPruning(get_lowest_node(possibleValue), possibleValue)</span><br><span class="line">        <span class="comment"># 判断是否有解</span></span><br><span class="line">        <span class="keyword">if</span> possibleValue <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">            display(possibleValue)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            print(<span class="string">"无解"</span>)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> judge_result(possibleValue):</span><br><span class="line">            print(<span class="string">"结果异常"</span>)</span><br><span class="line"></span><br><span class="line">        c += <span class="number">1</span></span><br><span class="line">        <span class="keyword">if</span> c &gt;= <span class="number">90</span>:</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">    end = time.time()</span><br><span class="line">    print(end - start)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 读取本地存储文件</span></span><br><span class="line">infile = open(<span class="string">"D:/top95.txt"</span>)</span><br><span class="line">main()</span><br></pre></td></tr></table></figure>

<br>

<h2 id="扩展"><a href="#扩展" class="headerlink" title="扩展"></a>扩展</h2><p>&emsp;&emsp;数独求解的算法，上面这种并不是最快的，还有一种叫做<strong>舞蹈链（Dancing Links）</strong>的算法，效率更高，有兴趣的可以了解一下；</p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2019-12-26T10:26:04.313Z" itemprop="dateUpdated">2019-12-26 18:26:04</time>
</span><br>


        
        世界是个球，前方总有路！
        
    </div>
    
    <footer>
        <a href="http://tewuyiang.gitee.io/blog">
            <img src="/blog/img/avatar.jpg" alt="特务依昂">
            特务依昂
        </a>
    </footer>
</blockquote>

        


        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" rel="tag">数据结构与算法</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/&title=《高效算法求解数独》 — 博客主页&pic=http://tewuyiang.gitee.io/blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/&title=《高效算法求解数独》 — 博客主页&source=一个未来程序员的博客~~~" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《高效算法求解数独》 — 博客主页&url=http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/&via=http://tewuyiang.gitee.io/blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/Java%E5%AE%9E%E7%8E%B0%E5%A0%86%E6%8E%92%E5%BA%8F/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Java实现堆排序</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/Java%E5%88%9B%E5%BB%BAList%E5%AF%B9%E8%B1%A1%E5%90%8C%E6%97%B6%E8%BF%9B%E8%A1%8C%E8%B5%8B%E5%80%BC%E6%93%8D%E4%BD%9C/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Java创建List、Map等集合对象的同时进行赋值操作</h4>
      </a>
    </div>
  
</nav>



    




















</article>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/blog/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>特务依昂 &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/&title=《高效算法求解数独》 — 博客主页&pic=http://tewuyiang.gitee.io/blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/&title=《高效算法求解数独》 — 博客主页&source=一个未来程序员的博客~~~" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《高效算法求解数独》 — 博客主页&url=http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/&via=http://tewuyiang.gitee.io/blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://tewuyiang.gitee.io/blog/%E9%AB%98%E6%95%88%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3%E6%95%B0%E7%8B%AC/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: false };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '人呢，怎么不见了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!欢迎回来!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
